package org.gwt.grin.rebind.input;

import org.gwt.grin.client.input.VisualRCHandler;
import org.gwt.grin.client.util.Rectangle;
import org.gwt.grin.rebind.AbstractSENode;
import org.gwt.grin.rebind.SECommand;
import org.gwt.grin.rebind.SEFeature;
import org.gwt.grin.rebind.SENode;
import org.gwt.grin.rebind.ShowBuilder;
import org.gwt.grin.rebind.features.SEAssembly;

import com.google.gwt.uibinder.rebind.IndentedWriter;

public class SEVisualRCHandler extends AbstractSENode implements SENode,
		SERCHandler {

	private final String[] gridNames;
	private final String[] stateNames;
	private final int[][] upDownAlternates;
	private final int[][] rightLeftAlternates;
	private final SECommand[][] selectCommands;
	private final SECommand[][] activateCommands;
	private final Rectangle[] mouseRects;
	private final int[] mouseRectStates;
	private final int timeout;
	private final SECommand[] timeoutCommands;
	private final boolean startSelected;
	private SEAssembly assembly;
	private SEFeature[] selectFeatures;
	private SEFeature[] activateFeatures;

	public SEVisualRCHandler(String name, String[] gridNames,
			String[] stateNames, int[][] upDownAlternates,
			int[][] rightLeftAlternates, SECommand[][] selectCommands,
			SECommand[][] activateCommands, Rectangle[] mouseRects,
			int[] mouseRectStates, int timeout, SECommand[] timeoutCommands,
			boolean startSelected) {
		super(name);
		this.gridNames = gridNames;
		this.stateNames = stateNames;
		this.upDownAlternates = upDownAlternates;
		this.rightLeftAlternates = rightLeftAlternates;
		this.selectCommands = selectCommands;
		this.activateCommands = activateCommands;
		this.mouseRects = mouseRects;
		this.mouseRectStates = mouseRectStates;
		this.timeout = timeout;
		this.timeoutCommands = timeoutCommands;
		this.startSelected = startSelected;
	}

	@Override
	public void setup(IndentedWriter w, ShowBuilder builder) {
		String vn = getVN(builder);
		w.write("%s.setup(%s,%s,%s,%s,%s,%s,%s,%s,%d,%s,%b,%s,%s,%s);", 
				vn,
				makeStringArray(gridNames), 
				makeStringArray(stateNames),
				makeIntArray(upDownAlternates),
				makeIntArray(rightLeftAlternates),
				makeCommandsArray(builder, selectCommands),
				makeCommandsArray(builder, activateCommands),
				makeRectangleArray(mouseRects), 
				makeIntArray(mouseRectStates),
				timeout, 
				makeCommandsArray(builder, timeoutCommands),
				startSelected, 
				assembly.getVN(builder),
				makeFeaturesArray(builder, selectFeatures),
				makeFeaturesArray(builder, activateFeatures));
	}

	@Override
	public int getKeyPressedInterestMask(SERCHandler h) {
		return VisualRCHandler.MASK;
	}

	@Override
	public int getKeyReleasedInterestMask(SERCHandler h) {
		return 0;
	}

	@Override
	protected String getClassName() {
		return VisualRCHandler.class.getName();
	}

	public void setup(SEAssembly assembly, SEFeature[] selectFeatures,
			SEFeature[] activateFeatures) {
		this.assembly = assembly;
		this.selectFeatures = selectFeatures;
		this.activateFeatures = activateFeatures;
	}

}
